<!--
 * @FilePath     : /study_code/vue2.x/component-set.html
 * @Description  : $set 追加字段
 * @Date         : 2025-05-02 20:53:12
 * @Author       : Fang YingGui
 * @LastEditors  : fangyinggui 18715384437@163.com
 * @LastEditTime : 2025-05-02 20:53:12
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>documentName</title>
    <link rel="stylesheet" href="" />
  </head>
  <body>
    <div id="app">
      <p>{{ userInfo.name }}</p>
      <p>{{ userInfo.age || 'empty' }}</p>
      <button @click="updateName">修改userInfo</button>
    </div>

    <script src="/vue/dist/vue.min.js"></script>
    <script src="./common.js"></script>
    <script>
      // 自定义组件
      const myComponent = {
        data: function () {
          return {}
        },
        props: [],
        template: ``,
        methods: {},
      }

      var app = new Vue({
        el: '#app',
        data: function () {
          return {
            userInfo: {
              name: 'hong',
            },
          }
        },
        methods: {
          updateName() {
            /* this.userInfo.age = '30'
          console.log('修改后的值', this.userInfo);
          this.$forceUpdate() */
            // tip: 需要追加到空的响应式数据（userInfo已是，否则需要初始化一个空的对象）
            this.$set(this.userInfo, 'age', 30)
          },
        },
      })
      /*
    直接给 userInfo 添加值，添加后，不是响应式的，所以页面不更新；
    */
    </script>
  </body>
</html>
